# Dickstein, Ho, and Mark (2023)
# Re: This script sets parameters and runs counterfactuals

# * # * # * # * # * # * #
# PRELIMINARIES         #
# * # * # * # * # * # * #

# Are you using a fixed markup loop? 
markuploop = T
if(markuploop == T){print("You are using the fixed markup loop")}

# Are you providing your own parameters?
load_params = T
if(load_params == T){print("You are loading parameters from the results, not providing your own parameters.")}
if(load_params == F){print("You are supplying your own parameters. This should include NamedBetaList, SGNamedBetaList, and SupplySideData")}

# Which Rating Areas? (You need to also change in DA02)
ravec = 1:7

# Which years?
yrvec = 2016

# What percent of the premium is covered by an employer?
nu = .65

# What is the fixed markup if not already set?
if(markuploop == F){
  mrkup <- 0
}

# What is the behavioral share
beshr <- .01

# What alpha-psi cutoff to use: 
alphampsicutoff <- .05

# What ACG high/low cutoff to use: 
acgHL_boundary <- 1.76

# Do you want to save the build counterfactual data or just the summary results?
if(markuploop == F){
  save_build_data <- F
}

# Where is the counterfactual folder?
if(markuploop == F){ 
  counterfactual_folder <- paste0(project_folder, "/analysis/counterfactuals")
}

# Which demand specification do you want to use?
if(markuploop == F){
  spectouse <- "lowrisk_simplemh_censor0.2"
}

# Where is the results file? 
if(markuploop == F){
  results_file <- paste0(counterfactual_folder, "/specs/", spectouse, "/output/results.txt")
}

# Set up folder for results
if(markuploop == F){
  dirtosave <- paste0(project_folder, "/analysis/tablesandfigures/release/counterfactuals/", spectouse)
}
dir.create(dirtosave, recursive = T)

# * # * # * # * # * # * # * #
# LOAD DATA AND FUNCTIONS   # 
# * # * # * # * # * # * # * #
source(paste0(counterfactual_folder, "/library/DA03aRunCounterfactualFunctions.R"))
source(paste0(counterfactual_folder, "/library/DA03bRunCounterfactualGetdataready.R"))

# ~ # ~ # ~ # ~ # ~ # ~ # ~ #
# RUN THE COUNTERFACTUALS   #  
# ~ # ~ # ~ # ~ # ~ # ~ # ~ #
source(paste0(counterfactual_folder, "/specs/", spectouse, "/DA03dCounterfactualCode.R"))

# ~ # ~ # ~ # ~ # ~ #
# GENERATING TABLES #
# ~ # ~ # ~ # ~ # ~ #
print(paste0("Saving results for ",  mrkup, " markup to ", dirtosave))

Results_Table <- list()
for(i in 1:2){
  Results_Table[[i]] <- cbind(
    unlist(resbase[[i]]), 
    unlist(res1[[i]]), 
    unlist(res2[[i]]), 
    unlist(res3[[i]]),
    unlist(res4[[i]]), 
    unlist(res5[[i]]))
  write.csv(Results_Table[[i]], paste0(dirtosave, c("/indmarketoutcomes.csv", "/sgmarketoutcomes.csv")[i]), 
    row.names = F) 
}

# ~ # ~ # ~ # ~ # ~ # ~ # ~ # ~ #
# SAVING BUILD DATA IF NEEDED   #
# ~ # ~ # ~ # ~ # ~ # ~ # ~ # ~ #
if(save_build_data == T){
  save(OnlyIndividualMarketCounterfactual, file = paste0(counterfactual_folder, "/specs/", spectouse, "/output/OnlyIndividualMarketCounterfactual_mrkup", round(100 * mrkup, 0), ".RData"))
  save(CombinedMarketCounterfactual1, file = paste0(counterfactual_folder, "/specs/", spectouse, "/output/CombinedMarketCounterfactual1_mrkup", round(100 * mrkup, 0), ".RData"))
  save(CombinedMarketCounterfactual6, file = paste0(counterfactual_folder, "/specs/", spectouse, "/output/CombinedMarketCounterfactual6_mrkup", round(100 * mrkup, 0), ".RData"))
  save(CombinedMarketCounterfactual7, file = paste0(counterfactual_folder, "/specs/", spectouse, "/output/CombinedMarketCounterfactual7_mrkup", round(100 * mrkup, 0), ".RData"))
}
